/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2016 abel533@gmail.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.myhouse.service.impl;


import com.myhouse.model.House;
import com.myhouse.repository.HouseRepository;
import com.myhouse.service.HouseService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

@Service
public class HouseServiceImpl implements HouseService {

    @Autowired
    private HouseRepository houseRepository;

    public List<House> getAll(House house) {
        return houseRepository.findAll();
    }

    public Page<House> getAllPage(House house) {
    	
    	//规格定义
        Specification<House> specification = new Specification<House>() {

            /**
             * 构造断言
             * @param root 实体对象引用
             * @param query 规则查询对象
             * @param cb 规则构建对象
             * @return 断言
             */
            @Override
            public Predicate toPredicate(Root<House> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicates = new ArrayList<>(); //所有的断言
                if (house.getAddr() != null && house.getAddr().length() > 0) {
                    Predicate likeAddr = cb.like(root.get("addr").as(String.class),"%" + house.getAddr()+"%");
                    predicates.add(likeAddr);
                }
                if (house.getCommunity() != null && house.getCommunity().length() > 0) {
                    Predicate likeCommunity = cb.like(root.get("community").as(String.class),"%" + house.getCommunity()+"%");
                    predicates.add(likeCommunity);
                }
                 
                return cb.and(predicates.toArray(new Predicate[0]));
            }
        };
        //分页信息
        
        Pageable pageable = PageRequest.of(house.getPage() -1 , house.getRows());
        //查询
        return houseRepository.findAll(specification,pageable);
        
    }
    
    public Optional<House> getById(Integer id) {
    	return houseRepository.findById(id);
    }

    public void deleteById(Integer id) {
    	houseRepository.deleteById(id);
    }

    public void save(House house) {
    	houseRepository.save(house);
    }
}

